const util= require('../../utils/util.js')
const app = getApp()
 let username=''
 let password=''
Page({
  data: {
    username: '',
    password: '',
    clientHeight:''
  },
  onLoad(){
    if(wx.getStorageSync('admin')){
        wx.showToast({
            title: '已登录',
            icon: 'success',
            duration: 2500,
            success:function(){
                setTimeout(function(){
                    wx.reLaunch({
                        url: '../index/index',
                    })
                },2500)
            }
        })
    }
    console.log(wx.getStorageSync('admin'))
    var that=this
    wx.getSystemInfo({ 
      success: function (res) { 
        console.log(res.windowHeight)
          that.setData({ 
              clientHeight:res.windowHeight
        }); 
      } 
    }) 
  },
  username(e){
    username=e.detail.value
  },
  password(e){
    password=e.detail.value
  },
  
  goadmin(){
    let flag = false  //表示账户是否存在,false为初始值
    if(username==''||!username)
    {
      wx.showToast({
        icon:'none',
        title: '账号不能为空',
      })
    }else if(password==''||!password){
      wx.showToast({
        icon:'none',
        title: '密码不能为空',
      })
    }else{
        util.http('/wx/getusers',resp=>{
            let admin=resp
            for (let i = 0; i < admin.length; i++) {  //遍历数据库对象集合
                if (username === admin[i].username) { //账户存在
                  flag=true;
                  if (password !== admin[i].password) {  //判断密码正确与否
                    wx.showToast({  //显示密码错误信息
                      title: '密码错误',
                      icon: 'error',
                      duration: 2500
                    });
                   break;
                  } else {
                    if(admin[i].status === 0){
                        wx.showToast({
                            title: '该账户已冻结',
                            icon: 'error',
                            duration: 2500
                            })
                            break;
                    }
                    wx.showToast({  //显示登录成功信息
                      title: '登陆成功',
                      icon: 'success',
                      duration: 2500,
                      success:function(){
                          setTimeout(function(){
                            wx.redirectTo({
                                url: '../index/index',
                            })
                          },1500)
                      }
                    })
                    flag=true;
                    wx.setStorageSync('admin', admin[i].id)
                    break;
                  }
                }
              };
              if(flag==false)//遍历完数据后发现没有该账户
              {
                wx.showToast({
                  title: '该用户不存在',
                  icon: 'error',
                  duration: 2500
                })
              }
        })
    }
  },
})
 
